<?php
namespace app\tools\command\base;

use app\tools\model\base\MenuNodesModel;
use app\tools\service\base\NodeService;
use think\admin\Command;
use think\admin\model\SystemMenu;
use think\console\Input;
use think\console\input\Argument;
use think\console\Output;

class UpdateNodeService extends Command
{
    protected function configure()
    {
        $this->setName('updateMenuNode')->setDescription('跟新菜单节点');

    }
    /**
     * xml订单入表并推送报关
     * @param Input $input
     * @param Output $output
     * @throws \think\admin\Exception
     */
    protected function execute(Input $input, Output $output)
    {
        SystemMenu::mk()->where('status', 1)->chunk(10, function ($menu) {

            foreach ($menu as $item) {

                $item->node && $this->insertData($item->node, $item->id);
            }
        });


    }


    private function insertData ($node, $id)
    {

        // 获得所在控制器的所有节点
        $data = NodeService::getNodes($node);

        if ( empty($data) ) {
            return true;
        }
        $node_arr = MenuNodesModel::mk()->where('menu_id', $id)->column('node');

        $list = [];
        // 获得表中已记录的节点
        foreach ($data as $key => $item) {

            if ( !in_array($key, $node_arr)) {
                $list[] = ['node'=>$key, 'title' => $item['title'] ,'is_selected'=>1, 'menu_id'=>$id];
            }
        }

        if (!empty($list)) {
            MenuNodesModel::mk()->insertAll($list);
        }

    }
}
?>